from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import sys
import time

# ANSI 转义码定义
RESET = "\033[0m"      # 重置所有属性
# 前景色
BLACK = "\033[30m"
RED = "\033[31m"
GREEN = "\033[32m"
YELLOW = "\033[33m"
BLUE = "\033[34m"
MAGENTA = "\033[35m"
CYAN = "\033[36m"
WHITE = "\033[37m"



now_side = -1
# 各国军队的英文缩写 to 中文国家
side_eng_to_cn = {'':'', 'PLA':'中国', 'PLANMC':'中国海军陆战队', 'USA':'美国',
                  'USMC':'美国海军陆战队', 'RUS':'俄罗斯', 'VDV':'俄罗斯空降兵',
                  'GB':'英国', 'CAF':'加拿大', 'AUS':'澳大利亚', 'INS':'老乡',
                  'MEA':'中东联盟', 'IMF':'民兵'}
# 输入地图, 模式,
def get_map(arg_map, arg_mode, arg_version, arg_side, update):
    print(arg_map, arg_mode, arg_version)
    """

    :param arg_map:      地图信息
    :param arg_mode:     模式信息
    :param arg_version:  版本信息
    :param arg_side:     阵营信息
    :param update:       是更新这张地图?还是询问下张地图? True表示更新,False表示询问
    :return:
    """

    # TODO: xpath可以用join方法优雅地写出来?

    special_zero = '' # 因为他妈的如果地图大于10，就会变成0几，所以要特判某些地图
    if int(arg_version) < 10:
        if arg_map == 'Gorodok' or arg_map == 'Kohat' or arg_map == 'Manicouagan' or arg_map == 'Yehorivka':
            special_zero = '0'

    xpath = "//section[@id='" + arg_map + "']/div/div/h3[text()='" + arg_mode + ' v' + special_zero  +arg_version + "']" + "/../a[@class='vehicles']"
    # print('xpath :', xpath)

    elem = driver.find_element(By.XPATH, xpath)
    # 路径是对的，但是点不了，报错说被点成其他元素，换一种写法就行了
    # stackoverflow 的玄学解答 : https://stackoverflow.com/questions/57741875/selenium-common-exceptions-elementclickinterceptedexception-message-element-cl
    driver.execute_script("arguments[0].click();", elem)
    # elem.click()

    this_map_side = -1 # 这张地图是哪边的
    next_map_side = -1 # 下张地图是哪边的
    team1_name = driver.find_element(By.XPATH, '//h3[@id="modal_team_1_name"]').text
    team2_name = driver.find_element(By.XPATH, '//h3[@id="modal_team_2_name"]').text
    # print(team1_name, team2_name, arg_side)
    if arg_side == team1_name:
        this_map_side = 1
        next_map_side = 2
    else:
        if update is True: # 还处于更新这张地图状态
            assert arg_side == team2_name
        this_map_side = 2
        next_map_side = 1
    if update is False: # 处于查询下张地图状态
        print('------------------------')
        if arg_side == 1:
            this_map_side = 1
            print(MAGENTA + '下一场你是', side_eng_to_cn[team1_name], '阵营' + RESET)
        else:
            assert arg_side == 2
            this_map_side = 2
            print(MAGENTA + '下一场你是 ' + side_eng_to_cn[team2_name] + ' 阵营' + RESET)
    # 获取当前页面的HTML源码
    # page_source = driver.page_source
    # file = open('./http_content.txt', 'w') # 保存HTTP报文信息
    # file.write(page_source)
    # print(page_source) # 输出HTTP报文信息
    # print('check :', "//div[@class='modal_side'][" + str(this_map_side) + "]/ul/li")
    parent_element = driver.find_elements(By.XPATH, "//div[@class='modal_side'][" + str(this_map_side) + "]/ul/li")
    if update is False:
        for i, element in enumerate(parent_element):
            print(GREEN + element.text[1:] + RESET)  # 第一位是载具个数,我是彩笔,先这么写
        # print(len(parent_element))
    return parent_element, next_map_side

# 浏览器启动选项
option = webdriver.ChromeOptions()
# 这行代码可以解决无界面模式下，读不出某些载具的问题，idk why LOL
option.add_argument("--window-size=1920,1080")
# # 指定为无界面模式
option.add_argument('--headless')
# option.add_argument("--disable-gpu") # 禁用gpu

driver = webdriver.Chrome(options=option)

driver.get("https://squadmaps.com")
# assert "Python" in driver.title
# print('driver.title :', driver.title)

def show_map_choice(): # 显示可选地图
    print('[1] Anvil'); print('[2] Al Barsrah'); print('[3] Belaya Pass')
    print('[4] Chora'); print('[5] Fallujah'); print('[6] Fool\'s Road')
    print('[7] Goose Bay'); print('[8] Gorodok'); print('[9] Kamdesh Highlands')
    print('[10] Kohat Toi'); print('[11] Kokan'); print('[12] Lashkar Valley')
    print('[13] Logar Valley'); print('[14] Manicouagan'); print('[15] Mestia')
    print('[16] Mutaha'); print('[17] Narva'); print('[18] Skorpo')
    print('[19] Sumari Bala'); print('[20] Tallil Outskirts'); print('[21] Yehorivka')
    print('[22] Black Coast');print('[23] Harju')
def show_mode_choice(): # 显示可选模式
    print('[1] RAAS'); print('[2] AAS'); print('[3] Invasion'); print('[4] Seed')
    print('[5] Tanks'); print('[6] Seed'); print('[7] TC')



map_list = ['', 'Anvil', 'AlBasrah', 'Belaya', 'Chora', 'Fallujah', 'FoolsRoad',
            'GooseBay', 'Gorodok', 'Kamdesh', 'Kohat', 'Kokan', 'Lashkar', 'Logar',
            'Manicouagan', 'Mestia', 'Mutaha', 'Narva', 'Skorpo', 'Sumari', 'Tallil',
            'Yehorivka', 'BlackCoast', 'Harju'] # [1, 23]

mode_list = ['', 'RAAS', 'AAS', 'Invasion', 'Seed', 'Tanks', 'Seed', 'TC'] # [1,7]

side_list = ['', 'PLA', 'PLANMC', 'USA', 'USMC', 'RUS', 'VDV', 'GB', 'CAF',
             'AUS', 'INS', 'MEA', 'IMF'] # [1, 12]


now_map_info = '' # 当前地图信息，后面确认用

print(CYAN + '请输入本张地图序号: (输入前面的编号即可)' + RESET)
show_map_choice()
# map = convert_map(int(input()))
map = map_list[int(input())]
now_map_info += map + ' '

print(CYAN + '请输入本场模式: (输入前面的编号即可)' + RESET)
show_mode_choice()
mode = mode_list[int(input())]
now_map_info += mode + ' '

print(CYAN + '请输入本张地图版本 v多少: (比如v8的话,输入8即可)' + RESET)
version = input()
now_map_info += 'v' + version + ' '

print(CYAN + '请输入你现在所在阵营:(输入前面的编号即可)' + RESET)
show_side_choice()
side = side_list[int(input())]

# 确认当前地图信息
user_input = input(RED + '本场地图信息 ' + now_map_info + ' 阵营:' + side  + '\n本场比赛信息正确嘛？(y/n): ' + RESET)
if user_input.lower() == "y":
    pass
else:
    sys.exit()




# 查询
_, next_map_side = get_map(map, mode, version, side, update=True)

"""
  接下来开始推测下一张图你在哪个阵营
"""
# 先把打开的阵营框去掉
driver.refresh()

next_map_info = '' # 下一张地图信息，后面确认用

print(CYAN + '请输入下一张地图序号: (输入前面的编号即可)' + RESET)
show_map_choice()
map = map_list[int(input())]
next_map_info += map + ' '

print(CYAN + '请输入下一场模式: (输入前面的编号即可)' + RESET)
show_mode_choice()
mode = mode_list[int(input())]
next_map_info += mode + ' '

print(CYAN + '请输入本张地图版本 v多少: (比如v8的话,输入8即可)' + RESET)
version = input()
next_map_info += 'v' + version

# 确认下一张地图信息
user_input = input(RED + '本场地图信息 ' + now_map_info + ' 阵营:' + side + \
'\n下一张地图信息 ' + next_map_info + '\n下一场比赛信息正确嘛？(y/n): ' + RESET)
if user_input.lower() == "y":
    pass
else:
    sys.exit()


_, ret = get_map(map, mode, version, next_map_side, update=False)



# elem.clear()
# elem.send_keys("pycon")
# elem.send_keys(Keys.RETURN)
# assert "No results found." not in driver.page_source
# driver.close()